home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Zoners Half-Life Tools / template / EndianMath.h < prev    next >
C/C++ Source or Header  |  2000-09-11  |  5KB  |  186 lines

  1. // Copyright (C) 2000  Sean Cavanaugh
  2. // This file is licensed under the terms of the Lesser GNU Public License
  3. // (see LPGL.txt, or http://www.gnu.org/copyleft/lesser.txt)
  4.  
  5. #ifndef ENDIAN_H__
  6. #define ENDIAN_H__
  7.  
  8. #if _MSC_VER > 1000
  9. #pragma once
  10. #endif // _MSC_VER > 1000
  11.  
  12. #include "basictypes.h"
  13. #include "BaseMath.h"
  14.  
  15. class Endian
  16. {
  17. public:
  18.     inline static INT16 FASTCALL Flip(const INT16 x)
  19.     {
  20.         INT16 a = (x >> 8) & 0x000FF;
  21.         INT16 b = (x << 8) & 0x0FF00;
  22.         INT16 rval = (a | b);
  23.         return rval;
  24.     }
  25.  
  26.     inline static UINT16 FASTCALL Flip(const UINT16 x)
  27.     {
  28.         UINT16 a = (x >> 8) & 0x000FF;
  29.         UINT16 b = (x << 8) & 0x0FF00;
  30.         UINT16 rval = (a | b);
  31.         return rval;
  32.     }
  33.  
  34.     inline static INT32 FASTCALL Flip(const INT32 x)
  35.     {
  36.         INT32 a = (x >> 24) & 0x0000000FF;
  37.         INT32 b = (x >> 8)  & 0x00000FF00;
  38.         INT32 c = (x << 8)  & 0x000FF0000;
  39.         INT32 d = (x << 24) & 0x0FF000000;
  40.         INT32 rval = (a | b | c | d);
  41.         return rval;
  42.     }
  43.  
  44.     inline static UINT32 FASTCALL Flip(const UINT32 x)
  45.     {
  46.         INT32 a = (x >> 24) & 0x0000000FF;
  47.         INT32 b = (x >> 8)  & 0x00000FF00;
  48.         INT32 c = (x << 8)  & 0x000FF0000;
  49.         INT32 d = (x << 24) & 0x0FF000000;
  50.         INT32 rval = (a | b | c | d);
  51.         return rval;
  52.     }
  53. #if 0
  54.     inline static INT64 FASTCALL Flip(const INT64 x)
  55.     {
  56.         INT64 a = (x >> 56) & 0x000000000000000FF;
  57.         INT64 b = (x >> 40) & 0x0000000000000FF00;
  58.         INT64 c = (x >> 24) & 0x00000000000FF0000;
  59.         INT64 d = (x >> 8 ) & 0x000000000FF000000;
  60.         INT64 e = (x << 8 ) & 0x0000000FF00000000;
  61.         INT64 f = (x << 24) & 0x00000FF0000000000;
  62.         INT64 g = (x << 40) & 0x000FF000000000000;
  63.         INT64 h = (x << 56) & 0x0FF00000000000000;
  64.         INT64 rval = (a | b | c | d | e | f | g | h);
  65.         return rval;
  66.     }
  67.  
  68.     inline static UINT64 FASTCALL Flip(const UINT64 x)
  69.     {
  70.         UINT64 a = (x >> 56) & 0x000000000000000FF;
  71.         UINT64 b = (x >> 40) & 0x0000000000000FF00;
  72.         UINT64 c = (x >> 24) & 0x00000000000FF0000;
  73.         UINT64 d = (x >> 8 ) & 0x000000000FF000000;
  74.         UINT64 e = (x << 8 ) & 0x0000000FF00000000;
  75.         UINT64 f = (x << 24) & 0x00000FF0000000000;
  76.         UINT64 g = (x << 40) & 0x000FF000000000000;
  77.         UINT64 h = (x << 56) & 0x0FF00000000000000;
  78.         UINT64 rval = (a | b | c | d | e | f | g | h);
  79.         return rval;
  80.     }
  81. #endif
  82.     inline static float FASTCALL Flip(const float x)
  83.     {
  84.         union floatflipper
  85.         {
  86.             struct _x_t
  87.             {
  88.                 BYTE    _v[4];
  89.             } _x;
  90.             float   _f;
  91.         };
  92.  
  93.         floatflipper tmp;
  94.         tmp._f = x;
  95.         SWAP(tmp._x._v[0], tmp._x._v[3]);
  96.         SWAP(tmp._x._v[1], tmp._x._v[2]);
  97.         return tmp._f;
  98.     }
  99.  
  100.     inline static double FASTCALL Flip(const double x)
  101.     {
  102.         union floatflipper
  103.         {
  104.             struct _x_t
  105.             {
  106.         BYTE    _v[8];
  107.             } _x;
  108.             double  _d;
  109.         };
  110.  
  111.         floatflipper tmp;
  112.         tmp._d = x;
  113.         SWAP(tmp._x._v[0], tmp._x._v[7]);
  114.         SWAP(tmp._x._v[1], tmp._x._v[6]);
  115.         SWAP(tmp._x._v[2], tmp._x._v[5]);
  116.         SWAP(tmp._x._v[3], tmp._x._v[4]);
  117.         return tmp._d;
  118.     }
  119.  
  120.     inline static void FlipArray(unsigned size, INT16* x)
  121.     {
  122.         for (unsigned i=0 ; i<size ; i++, x++)
  123.         {
  124.             *x = Flip(*x);
  125.         }
  126.     }
  127.  
  128.     inline static void FlipArray(unsigned size, UINT16* x)
  129.     {
  130.         for (unsigned i=0 ; i<size ; i++, x++)
  131.         {
  132.             *x = Flip(*x);
  133.         }
  134.     }
  135.  
  136.     inline static void FlipArray(unsigned size, INT32* x)
  137.     {
  138.         for (unsigned i=0 ; i<size ; i++, x++)
  139.         {
  140.             *x = Flip(*x);
  141.         }
  142.     }
  143.  
  144.     inline static void FlipArray(unsigned size, UINT32* x)
  145.     {
  146.         for (unsigned i=0 ; i<size ; i++, x++)
  147.         {
  148.             *x = Flip(*x);
  149.         }
  150.     }
  151. #if 0
  152.     inline static void FlipArray(unsigned size, INT64* x)
  153.     {
  154.         for (unsigned i=0 ; i<size ; i++, x++)
  155.         {
  156.             *x = Flip(*x);
  157.         }
  158.     }
  159.     inline static void FlipArray(unsigned size, UINT64* x)
  160.     {
  161.         for (unsigned i=0 ; i<size ; i++, x++)
  162.         {
  163.             *x = Flip(*x);
  164.         }
  165.     }
  166. #endif
  167.  
  168.     inline static void FlipArray(unsigned size, float* x)
  169.     {
  170.         for (unsigned i=0 ; i<size ; i++, x++)
  171.         {
  172.             *x = Flip(*x);
  173.         }
  174.     }
  175.  
  176.     inline static void FlipArray(unsigned size, double* x)
  177.     {
  178.         for (unsigned i=0 ; i<size ; i++, x++)
  179.         {
  180.             *x = Flip(*x);
  181.         }
  182.     }
  183. };
  184.  
  185. #endif // ENDIAN_H__
  186.